#include <stdio.h>
#include <getopt.h>

int main(int argc, char* argv[])
{
    int c = 0;
    int option_index;
    int flag = -1;

    struct option long_options[] = {
        {"add",     required_argument, &flag, 'a'}, 
        {"delete",  required_argument, &flag, 'd'},
        {"clear",   required_argument, &flag, 'c'},
        {"list",    required_argument, &flag, 'l'},
        {0,         0,                 0,      0 }
    };

    while (1) {
        printf("\noptind: %d, option_index: %d\n", optind, option_index);

        int c = getopt_long(argc, argv, "n:ml::", long_options, &option_index);
        if (c == -1)
            break;

        printf("flag = %d(%c), c = %d(%c)\n", flag, flag, c, c);

        if (c) {
            if (c != '?') {
                printf("short: option: %c, value: %s\n", c, optarg);
            }
        } else {
            printf("long: option: %s, value: %s\n", long_options[option_index].name, optarg);
        }
    }

    if (optind < argc) {
        printf("parameters:\n");
        while (optind < argc) {
            printf("%s\n", argv[optind++]);
        }
    }

    return 0;
}
